var rootPath = getRootPath();

$(function() {
	$.ajax({
		url: rootPath + '/loginState/queryUserLoginStateExceptions',
        type: 'POST',
        contentType : 'application/json;charset=UTF-8',
        data: JSON.stringify({
        	"dr" : 1
        }),
        success: function (data) {
        	fillLoginState(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error:(");
        }
    });
	
	$("#searchLoginState").click(function() {
		$.ajax({
			url: rootPath + '/loginState/queryUserLoginStateExceptions',
	        type: 'POST',
	        contentType : 'application/json;charset=UTF-8',
	        data: JSON.stringify({
	        	"uid" : $("#uid").val(),
	        	"dr" : 1
	        }),
	        success: function (data) {
	        	fillLoginState(data);
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	layer.alert("error:(");
	        }
	    });
	});
});

function loginState_del(obj,id){
	var dataNum = parseInt($("#dataNum").html());
	layer.confirm('登录异常名单删除须谨慎，确认要删除吗？',function(index){
		if(id != "") {
			$.ajax({
				url: rootPath + '/loginState/deleteLoginState',
				type: 'POST',
				data : {
					"uid" : id
				},
				success: function(data){
					if(data.returnCode == "0000") {						
						$(obj).parents("tr").remove();
						layer.msg('已删除!',{icon:1,time:1000});
						
						$("#dataNum").html(dataNum - 1);
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error:function(data) {
					layer.alert("error:(");
				},
			});		
		}
	});
}

function switchPage(pageNo) {
	$(this).css("cursor", "pointer");
	$.ajax({
		url : rootPath + '/loginState/queryUserLoginStateExceptions',
		type : 'POST',
		contentType : 'application/json;charset=UTF-8',
		data : JSON.stringify({
			"dr" : 1,
			"pageNo" : pageNo
		}),
		success : function(data) {
			fillLoginState(data);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});
}

function fillLoginState(data) {
	if(data.returnCode == "0000") {
		var length = data.returnObject.list.length;
		$("#dataNum").html(data.returnObject.total);
		var tr;
		for(var i = 0;i < length;i++) {
			tr += "<tr class='text-c' name='loginStateTr'>";
			tr += "<td>" + data.returnObject.list[i].uid + "</td>";
			tr += "<td>" + formatUndefined(data.returnObject.list[i].uname) + "</td>";
			tr += "<td>" + formatUndefined(data.returnObject.list[i].phone) + "</td>";
			tr += "<td>" + formatUndefined(data.returnObject.list[i].email) + "</td>";
			tr += "<td>" + data.returnObject.list[i].wrongTimes + "</td><td>";
//			tr += "<a title='编辑' href='javascript:;' onclick='loginState_add_edit(\"编辑用户异常名单\"," + "\"loginStateEditPage?loginStateId=" + data.returnObject.list[i].loginStateId + "\", \"\",\"\")' style='text-decoration:none'><i class='Hui-iconfont'>&#xe6df;</i></a>";
			tr += "<a title='删除1' href='javascript:;' onclick='loginState_del(this," + "\"" + data.returnObject.list[i].uid + "\")' class='ml-5' style='text-decoration:none'><i class='iconfont marginLeft12'>&#xe612;</i></a></td></tr>";
		}
		$("#loginStates").empty();
		$("#loginStates").append(tr);
		
		var page = "";
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ 1 + ")'>首页</span>";
		if (data.returnObject.hasPreviousPage) {
			page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.prePage + ")'>上一页</span>";
		}
		var num = data.returnObject.navigatepageNums.length;
		for (var i = 0; i < num; i++) {
			if (data.returnObject.navigatepageNums[i] == data.returnObject.pageNum) {
				page += "<span style='font-weight:900;padding:7px 10px;'>"
						+ data.returnObject.pageNum + "</span>";
			} else {
				page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
						+ data.returnObject.navigatepageNums[i]
						+ ")'>"
						+ data.returnObject.navigatepageNums[i] + "</span>";
			}
		}
		if (data.returnObject.hasNextPage) {
			page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.nextPage + ")'>下一页</span>";
		}
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ data.returnObject.pages + ")'>末页</span>";

		$("#page").empty();
		$("#page").append(page);
		$(".pageNavigate").each(function() {
			$(this).hover(function() {
				$(this).css("cursor","pointer");
			});
		});
	} else {
		$("#loginStates").empty();
		$("#dataNum").html("0");
	}
}